<?php

namespace App\Http\Controllers\Common;

use App\Service\WdhService;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Redis as Redis;

class RegionController extends Controller
{


    /**
     * 获取省
     * @return \Illuminate\Http\JsonResponse
     */
    public function getProvince(  )
    {
        $region_data = redisTransformation(redis::get('GLOBAL_REGION_CACHE'));

        if ( empty($region_data) ){
            $region_data = WdhService::getRegion();
        }

        if ( !isset($region_data['province']) || empty( $region_data['province'] ) ) {
            return response()->json(['code' => 100001, 'message' => '没有省份数据']);
        }

        $tamp_province = [];

        foreach ( $region_data['province'] as $item ) {
            $tamp_province[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'parentID' => !isset($item['parentID']) ? 0 : $item['parentID'],
                'region_level' => !isset($item['region_level']) ? 1 : $item['region_level'],
            ];
        }

        return response()->json(['code' => 200, 'message' => 'ok','data'=> $tamp_province ]);

    }


    /**
     * 获取市
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCity( Request $request )
    {

        $province_id = $request->input('pid');

        if ( empty( $province_id ) ) {
            return response()->json(['code' => 100001, 'message' => '缺少参数:pid']);
        }

        $region_data = redisTransformation(redis::get('GLOBAL_REGION_CACHE'));

        if ( empty($region_data) ){
            $region_data = WdhService::getRegion();
        }

        if ( !isset($region_data['city'][$province_id]) || empty( $region_data['city'][$province_id] ) ) {
            return response()->json(['code' => 100002, 'message' => '没有市区数据']);
        }

        $tamp_city = [];
        foreach ( $region_data['city'][$province_id] as $item ) {
            $tamp_city[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'parentID' => !isset($item['parentID']) ? 0 : $item['parentID'],
                'region_level' => !isset($item['region_level']) ? 2 : $item['region_level'],
            ];
        }

        return response()->json(['code' => 200, 'message' => 'ok','data'=> $tamp_city ]);

    }

    /**
     * 获取区
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCountry( Request $request )
    {

        $province_id = $request->input('pid');
        $city_id = $request->input('cid');

        if ( empty( $province_id ) ) {
            return response()->json(['code' => 100001, 'message' => '缺少参数:pid']);
        }

        if ( empty( $city_id ) ) {
            return response()->json(['code' => 100002, 'message' => '缺少参数:cid']);
        }

        $region_data = redisTransformation(redis::get('GLOBAL_REGION_CACHE'));

        if ( empty($region_data) ){
            $region_data = WdhService::getRegion();
        }

        if ( !isset($region_data['county'][$province_id . $city_id] ) || empty( $region_data['county'][$province_id . $city_id]  ) ) {
            return response()->json(['code' => 100003, 'message' => '没有地区数据']);
        }

        $tamp_county = [];
        foreach ( $region_data['county'][$province_id . $city_id] as $item ) {
            $tamp_county[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'parentID' => !isset($item['parentID']) ? 0 : $item['parentID'],
                'region_level' => !isset($item['region_level']) ? 3 : $item['region_level'],
            ];
        }

        return response()->json(['code' => 200, 'message' => 'ok','data'=> $tamp_county ]);

    }


}
